/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.app.facade.Facade;
import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.OrderItem;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.services.ComputeOrderAmountService;
import com.startup.musicstore.services.crud.OrderCrudService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author aljay19
 */
@Service("computeOrderAmountService")
@Transactional
public class ComputeOrderAmountServiceImpl implements ComputeOrderAmountService {

    private Facade data = new Facade();
    
    @Override
    public BigDecimal getOrderAmount(Long orderId) {
        
        double total = 0;
        OrderCrudService orderCrudService = data.getOrderCrudService();
        
        Orders order = orderCrudService.findById(orderId);
        List<OrderItem> orderItems = order.getOrderItems();
        
        for(OrderItem oi: orderItems){
            Album album = oi.getAlbum();
            String unitPrice = album.getUnitPrice().toPlainString();
            total += oi.getQuantity() * Double.parseDouble(unitPrice);
        }
        
        return new BigDecimal(total);
    }
    
}
